/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.custom;

import java.io.File;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;

import javolution.util.FastMap;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import com.l2jserver.gameserver.templates.StatsSet;

/**
 * @author Laikeriz
 */
public class NpcToPcManager
{
	public static Map<Integer, StatsSet> pcs = new FastMap<Integer, StatsSet>();
	private static final Logger _log = Logger.getLogger(NpcToPcManager.class.getName());
	
	public static StatsSet getPcInfo(int id)
	{
		return pcs.get(id);
	}
	
	public static boolean doesPcExist(int id)
	{
		return pcs.containsKey(id);
	}
	
	public static void init()
	{
		try
		{
			pcs.clear();
			File file = new File("config/npc_to_pc.xml");
			Document doc = null;
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			factory.setIgnoringComments(true);
			doc = factory.newDocumentBuilder().parse(file);
			for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
			{
				if ("pcsList".equalsIgnoreCase(n.getNodeName()))
				{
					for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
					{
						if ("pc".equalsIgnoreCase(d.getNodeName()))
						{
							if (!Boolean.parseBoolean(d.getAttributes().getNamedItem("enabled").getNodeValue()))
								continue;
							StatsSet tempSet = new StatsSet();
							for (Node a = d.getFirstChild(); a != null; a = a.getNextSibling())
							{
								if ("#text".equalsIgnoreCase(a.getNodeName()))
									continue;
								String type = a.getAttributes().getNamedItem("type").getNodeValue();
								if (type.equalsIgnoreCase("string"))
									tempSet.set(a.getNodeName(), a.getAttributes().getNamedItem("value").getNodeValue());
								else if (type.equalsIgnoreCase("int"))
								{
									String temp = a.getAttributes().getNamedItem("value").getNodeValue();
									int value = (temp == "" || temp == null ? 0 : Integer.parseInt(temp));
									tempSet.set(a.getNodeName(), value);
								}
								else if (type.equalsIgnoreCase("boolean"))
								{
									String temp = a.getAttributes().getNamedItem("value").getNodeValue();
									boolean value = (temp == "" || temp == null ? false : Boolean.parseBoolean(temp));
									tempSet.set(a.getNodeName(), value);
								}
								if ("cubics".equalsIgnoreCase(a.getNodeName()))
									tempSet.set("cubicsSize", Integer.parseInt(a.getAttributes().getNamedItem("size").getNodeValue()));
							}
							pcs.put(Integer.parseInt(d.getAttributes().getNamedItem("npcId").getNodeValue()), tempSet);
						}
					}
				}
			}
			_log.info("NpcToPcManager: loaded " + pcs.size() + " PCs");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
